import { _decorator, Component, Node } from 'cc';
import { MatchSpace } from './MatchSpace';
import { MatchstickMain } from './MatchstickMain';
import { digitMap } from './matchstickDataMap';

const { ccclass, property } = _decorator;

@ccclass('MatchDigit')
export class MatchDigit extends Component {
	digValue: number | null;
	decade: boolean;
	value: number | null;

	start() {}

	initData(digList: Array<0 | 1>) {
		this.node.children.forEach((child, index) => {
			let matchSpace = child.getComponent(MatchSpace);
			matchSpace.initData(digList[index]);
		});
	}
	updateValue() {
		let children = this.node.children;
		let index = digitMap.findIndex((item) => {
			if (typeof item[0] === 'number') {
				//处理非1的数字
				return item.every(
					(dig, i) => Boolean(dig) === Boolean(children[i].children.length)
				);
			} else {
				return item.some((arr) => {
					return arr.every((dig, i) => {
						return Boolean(dig) === Boolean(children[i].children.length);
					});
				});
			}
		});
		this.digValue = index >= 0 ? (index === 10 ? -1 : index) : null;
		let parent = this.node.getParent();
		let main = parent.getComponent(MatchstickMain);
		main.digValueChanged();
	}
}
